<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ModController
 *
 * @author Maciej
 */
class ModController extends PanelController {
    protected $role = 'mod';
    public $metaKeywords = 'Panel Moderatora';
    public $metaDescription = 'Panel Moderatora';
    public $pageTitle = 'Panel Moderatora';
    
    public $menu = array(
        array(
            'name' => 'Dodaj konto',
            'link' => 'mod/createaccount',
        ),
        array(
            'name' => 'Zmiana hasła',
            'link' => 'client/changepassword',
        )
    );
    
    public function actionIndex(){
        $this->render('index', array());
    }
    public function actionChangePassword(){
        $model = new ChangePasswordForm();
        
        if(isset($_POST['ChangePasswordForm']))
        {
            $model->attributes = $_POST['ChangePasswordForm'];
            
            if($model->validate()){
                if($model->validate()){
                    if($model->changePassword(Yii::app()->user->id, 1) !== 1){
                        $model->addError('oldpassword','Nie udało się zmienić hasła.');
                    }else{
                        $this->redirect('mod/info');
                    }
                }
            }
        }

        $this->render('application.views.components.change_password', array('model' => $model));
    }
    
    public function actionInfo(){
        $this->render('application.views.components.info', array());
    }
    
    public function actionCreateAccount() {
       
        $model = new AccountForm();
        
        if(isset($_POST['AccountForm']))
        {
            $model->attributes = $_POST['AccountForm'];
            
            if($model->validate()){
                
                if($model->addAccount() === 0){
                    $model->addError('no_account','Numer już istnieje');
                }elseif($model->addAccount() === -1){
                    $model->addError('no_account','Niewłaściwy numer konta');
                }else{
                    $this->redirect(array('/admin'));
                }
            }
        }
        
        $this->render('account', array('model' => $model));
    }
}
